﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IntelligentCMS
{
    public partial class CMSTextBox : System.Web.UI.UserControl, iCMSControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            theTextBox.BorderColor = System.Drawing.Color.Gray;
            theTextBox.BackColor = System.Drawing.Color.White;
            theTextBox.ForeColor = System.Drawing.Color.Black;
            
           
        }

        

        #region iCMSControl Members

        private bool checkforerrors = false;
        private string name = string.Empty;



        public string Name
        {
            set
            {
                name = value;
            }
        }

        private List<string> errorlist()
        {
            List<string> errorlist = new List<string>();

            if (theTextBox.Text.Trim() == string.Empty && checkforerrors)
            {
                errorlist.Add(name + strings.fielderror);
                theTextBox.BorderColor = System.Drawing.ColorTranslator.FromHtml("#FF0000");
                theTextBox.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFCCCC");
                theTextBox.ForeColor = System.Drawing.ColorTranslator.FromHtml("#D92042");
            }
            else
            {
                theTextBox.BorderColor = System.Drawing.Color.Gray;
                theTextBox.BackColor = System.Drawing.Color.White;
                theTextBox.ForeColor = System.Drawing.Color.Black;


            }
            return errorlist;
        }

        private bool haserrors()
        {
            if (errorlist().Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }

        }

        private string key = string.Empty;

        public string Key
        {
           
            set
            {
                key = value;
            }


        }



        /*
        private string generatesqlstring()
        {
            string sqlstring = string.Empty;

            sqlstring += "INSERT INTO [" + strings.databaseprefix + "].[dbo].[" + strings.databaseprefix + "_TextBox] ([Name], [Value], [URL], [Time]) VALUES ('" + common.GetBase64String(name) + "','" + common.GetBase64String(theTextBox.Text) + "','" + common.GetBase64String(clienturl) + "', GETDATE());";

            return sqlstring;

        }
         * 
         * */

        private string clienturl = string.Empty;

        public string ClientURL
        {
            set
            {
                clienturl = value;
            }


        }

        public System.Web.UI.WebControls.Unit InternalWidth
        {
            set
            {
                theTextBox.Width = value;
            }
        }

        public System.Web.UI.WebControls.Unit InternalHeight
        {
            set
            {
                theTextBox.Height = value;
            }
        }

        public TextBoxMode TextMode 
        {
            set
            {
                theTextBox.TextMode = value;
            }
                
        }


        private string text = string.Empty;
        public List<string> Errors
        {
            get { return errorlist(); }
        }
        /*
        public string SQLString
        {
            get { return generatesqlstring(); }
        }
        */
        public string LabelText
        {
            set { theLabel.Text = value; }
        }

        public bool CheckForErrors
        {
            set { checkforerrors = value; }
        }

        public bool HasErrors
        {
            get { return haserrors(); }
        }

        #endregion


        private List<Pair> getPairs()
        {
            List<Pair> newList = new List<Pair>();

            newList.Add(new Pair(key, theTextBox.Text.Trim()));

            return newList;
        }


        #region iCMSControl Members


        public List<Pair> GetSqlValues
        {
            get { return getPairs(); }
        }

        #endregion
    }
}